package com.foosun.core.service;

import com.foosun.core.base.BaseDao;
import com.foosun.core.base.BaseService;
import com.foosun.core.entity.FileObject;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nelin on 14-2-21.
 */
@Service
@Transactional
public class FileObjectService extends BaseService<FileObject> {

    @Override
    @Resource(name = "fileObjectDao")
    public void setBaseDao(BaseDao<FileObject> baseDao) {
        this.baseDao = baseDao;
    }

    public List<FileObject> findListByIds(String idStr) throws Exception {

        String[] ids = idStr.split(",");
        List<Integer> idList = new ArrayList<Integer>();
        for (String id : ids) {
            idList.add(Integer.parseInt(id));
        }
        return super.findByIds(idList);
    }

    @Override
    public void delete(Integer id) throws Exception {
        FileObject fileObject = super.findById(id);
        try {
            FileUtils.forceDelete(new File(fileObject.getPath() + fileObject.getName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.delete(id);
    }
}
